package com.course.android.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.course.android.R;

import org.apache.commons.lang.StringUtils;

public class PositiveDialog extends Dialog {


    TextView titleTv, descTv, okBt, cancelBt;

    public PositiveDialog(Context context) {
        super(context, R.style.ToastDialog);
        View viewDialog = LayoutInflater.from(getContext()).inflate(R.layout.dialog_positive, null);
        titleTv = viewDialog.findViewById(R.id.titleTv);
        descTv = viewDialog.findViewById(R.id.descTv);
        okBt = viewDialog.findViewById(R.id.okBt);
        cancelBt = viewDialog.findViewById(R.id.cancelBt);
        viewDialog.findViewById(R.id.rootView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        descTv.setVisibility(View.GONE);
        cancelBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setContentView(viewDialog);
    }

    public PositiveDialog buildTitle(String title) {
        if (StringUtils.isNotBlank(title)) {
            titleTv.setText(title);
        }
        titleTv.setVisibility(StringUtils.isNotBlank(title) ? View.VISIBLE : View.GONE);
        return this;
    }

    public PositiveDialog buildDesc(String desc) {
        if (StringUtils.isNotBlank(desc)) {
            descTv.setText(desc);
        }
        descTv.setVisibility(StringUtils.isNotBlank(desc) ? View.VISIBLE : View.GONE);
        return this;
    }

    public PositiveDialog buildOkName(String okName) {
        if (StringUtils.isNotBlank(okName)) {
            okBt.setText(okName);
        }
        return this;
    }

    public PositiveDialog buildOkClick(final View.OnClickListener okClick) {
        if (okClick != null) {
            okBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    okClick.onClick(v);
                }
            });
        }
        return this;
    }

    public PositiveDialog buildCancleClick(final View.OnClickListener cancleClick) {
        if (cancleClick != null) {
            cancelBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    cancleClick.onClick(v);
                }
            });
        }
        return this;
    }

    public PositiveDialog buildCancleClick(String cancleText, final View.OnClickListener cancelClick) {

        cancelBt.setTextColor(Color.parseColor("#67C23A"));
        if (StringUtils.isNotBlank(cancleText)) {
            cancelBt.setText(cancleText);
        }
        if (cancelClick != null) {
            cancelBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    cancelClick.onClick(v);
                }
            });
        }
        return this;
    }

    public PositiveDialog bindData(Object param, String content) {
        this.param = param;
        return this;
    }


    public void initView(String title, String desc, String okBtName, View.OnClickListener okClick,
                         View.OnClickListener cancleClick) {
        if (StringUtils.isNotBlank(title)) {
            titleTv.setText(title);
        }
        titleTv.setVisibility(StringUtils.isNotBlank(title) ? View.VISIBLE : View.GONE);
        if (StringUtils.isNotBlank(desc)) {
            descTv.setText(desc);
        }
        descTv.setVisibility(StringUtils.isNotBlank(desc) ? View.VISIBLE : View.GONE);
        if (StringUtils.isBlank(okBtName)) {
            okBtName = "确定";
        }
        if (cancleClick != null) {
            cancelBt.setOnClickListener(cancleClick);
        }
        okBt.setText(okBtName);
        okBt.setOnClickListener(okClick);
    }

    Object param;


    @Override
    public void dismiss() {
        if (isShowing()) {
            super.dismiss();
        }
    }

    @Override
    public void show() {
        if (!isShowing()) {
            super.show();
        }
    }


}
